<?php
namespace app\web\controller;

use app\common\model\commonModel;
use app\common\model\trReason;
use app\common\model\trSystemSet;
use app\common\util\ErrorCode;
use app\web\service\UserContactService;
use think\Db;

class AttachController extends CommonController
{
    public function initialize()
    {
        $this->noCheckTokenAction = ["getCustomerServiceTel", "getRuleContent", "getDebugSetting", "updateDebugSetting", "getAboutUs"];
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function handOut($_type){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        switch ($_type){
            case "contactList": // 获取联系人
                $method = "getContactList";
                break;
            case "addContact": // 添加联系人
                $method = "toAddContact";
                break;
            case "detailContact": // 联系人信息
                $method = "getDetailContact";
                break;
            case "updateContact": // 更新联系人
                $method = "toUpdateContact";
                break;
            case "delContact": // 删除联系人
                $method = "toDelContact";
                break;
            case "getReason": //获取理由原因
                $method = "getReason";
                break;
            case "addFormId": //添加小程序的form_id
                $method = "addWxFormId";
                break;
            case "lineInfo": //获取线路信息
                $method = "getLineInfo";
                break;
            default:
                return $this->returnData = ErrorCode::$errorCode["fail"];
                break;
        }
        return $this->{$method}();
    }

    // 获取设置信息
    public function getDebugSetting(){
        $data = trSystemSet::getValue("wxAppDebugSetting");
        $data["enableDebug"] = isset($data["enableDebug"]) ? $data["enableDebug"] : false;
        $data["version"] = isset($data["version"]) ? $data["version"] : Db::name(wxAppUpgradeTable)->where("mall_id", request()->mallData["id"])->order("create_time", "desc")->value("version");
        $this->returnData["data"] = $data;
        return $this->return_result($this->returnData);
    }

    // 更新设置信息
    public function updateDebugSetting(){
        $result = trSystemSet::toSave([
            "enableDebug" => isset($this->params["enableDebug"]) ? $this->params["enableDebug"] : false,
            "version" => isset($this->params["version"]) ? $this->params["version"] : Db::name(wxAppUpgradeTable)->where("mall_id", request()->mallData["id"])->order("create_time", "desc")->value("version")
        ], "wxAppDebugSetting");
        if(!$result){
            $this->returnData = ErrorCode::$errorCode["fail"];
        }
        return $this->return_result($this->returnData);
    }

    // 关于我们
    public function getAboutUs(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $data = trSystemSet::getValue("aboutUsConfig");
        if(empty($data)){
            $data = [
                "aboutUs" => "",  //关于我们
            ];
        }
        $this->returnData["data"] = $data;
        return $this->return_result($this->returnData);
    }

    // 获取客服电话
    public function getCustomerServiceTel(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $data = trSystemSet::getValue("customerServiceConfig");
        if(empty($data)){
            $data = [
                "territoryTel" => "",  //境内电话
                "abroadTel" => "",  //境外电话
            ];
        }
        $this->returnData["data"] = [
            "previewData" => ["境内：{$data["territoryTel"]}", "境外：{$data["abroadTel"]}"],
            "mobileData" => [$data["territoryTel"], $data["abroadTel"]]
        ];
        return $this->return_result($this->returnData);
    }

    // 联系人列表
    public function getContactList(){
        $service = $this->app->invokeClass(UserContactService::class, [$this->returnData]);
        $this->returnData = $service->getAllData(request()->userLoginData["id"]);
        return $this->return_result($this->returnData);
    }

    // 添加联系人
    public function toAddContact(){
        $this->params["userId"] = request()->userLoginData["id"];
        $service = $this->app->invokeClass(UserContactService::class, [$this->returnData]);
        $this->returnData = $service->toAdd($this->params);
        return $this->return_result($this->returnData);
    }

    // 联系人详情
    public function getDetailContact(){
        $service = $this->app->invokeClass(UserContactService::class, [$this->returnData]);
        $this->returnData = $service->getDetail($this->params);
        return $this->return_result($this->returnData);
    }

    // 更新联系人
    public function toUpdateContact(){
        $this->params["userId"] = $this->app->request->userLoginData["id"];
        $service = $this->app->invokeClass(UserContactService::class, [$this->returnData]);
        $this->returnData = $service->toUpdate($this->params);
        return $this->return_result($this->returnData);
    }

    // 删除联系人
    public function toDelContact(){
        $service = $this->app->invokeClass(UserContactService::class, [$this->returnData]);
        $this->returnData = $service->toDel($this->params);
        return $this->return_result($this->returnData);
    }

    // 添加小程序的form_id
    public function addWxFormId(){
        $field = ["form_id" => "require", "type" => "", "mall_id" => request()->mallData["id"]];
        foreach ($field as $k => $v) {
            $ks = parse_name($k); //转换字段，变驼峰
            if($v === 'require' && !isset($this->params[$ks])){
                return $this->return_result(ErrorCode::$errorCode['params_not_exist'], $ks);
            }
            $data[$k] = isset($this->params[$ks]) ? $this->params[$ks] : $v;
        }
        if(strpos($this->params["formId"], "the formId") === false) { // 存 form_id
            $data["user_id"] = $this->app->request->userLoginData["id"];
            $data["create_time"] = time();
            commonModel::toAdd(formIdTable, $data);
        }
        return $this->return_result($this->returnData);
    }

    //获取理由原因
    public function getReason(){
        if(!isset($this->params["type"])){
            return $this->return_result(ErrorCode::code("params_not_exist", "type"));
        }
        if(!in_array($this->params["type"], [0,1,2,3,4,5])){ // 类型；1机场接送，2巴士路线，3跨境专车接送，4包车线路，0通用，5车辆租借
            return $this->return_result(ErrorCode::code("params_error", "type"));
        }
        if($this->params["type"] != "0") { // 加入通用类型
            $filter = array(["type", "in", $this->params["type"].",0"], ["status", "=", 1], ["mall_id", "=", request()->mallData["id"]]);
        }else{
            $filter = array("type" => $this->params["type"], "status" => 1, "mall_id" => request()->mallData["id"]);
        }
        $model = new trReason();
        $data = $model->getFields("id,content", $filter, false);
        $this->returnData['data'] = $data;
        return $this->return_result($this->returnData);
    }

    // 获取退改规则
    public function getRuleContent(){
        $data = trSystemSet::getValue("ruleContent");
        if(empty($data)){
            $data = [
                "ruleContent" => "",  //规则
            ];
        }
        $this->returnData["data"] = ["content" => $data["ruleContent"]];
        return $this->returnData;
    }

    // 获取线路信息
    public function getLineInfo(){
        $airportData = trSystemSet::getValue("airportRemarks");
        $enterpriseData = trSystemSet::getValue("enterpriseRemarks");
        $vehicleData = trSystemSet::getValue("vehicleRemarks");
        $carData = trSystemSet::getValue("carRemarks");
        // 类型；1机场接送，2固定路线，3专车接送，4包车路线
        $this->returnData["data"] = [
            ["alias" => !empty($carData["carAlias"]) ? $carData["carAlias"] : "专车接送线路", "type" => 3],
            ["alias" => !empty($vehicleData["vehicleAlias"]) ? $vehicleData["vehicleAlias"] : "包车线路", "type" => 4],
            ["alias" => !empty($airportData["airportAlias"]) ? $airportData["airportAlias"] : "机场线路", "type" => 1],
            ["alias" => !empty($enterpriseData["enterpriseAlias"]) ? $enterpriseData["enterpriseAlias"] : "巴士线路", "type" => 2],
            ["alias" => "车辆租凭", "type" => 5]
        ];
        return $this->returnData;
    }
}
